package com.ruoyi.master.mapper;

import com.ruoyi.master.domain.TTask;
import com.ruoyi.master.domain.WeightDriftRateVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 任务Mapper接口
 * 
 * @author jiandan
 * @date 2024-06-27
 */
public interface TTaskMapper 
{
    /**
     * 查询任务
     * 
     * @param taskId 任务主键
     * @return 任务
     */
    public TTask selectTTaskByTaskId(Long taskId);

    /**
     * 查询任务列表
     * 
     * @param tTask 任务
     * @return 任务集合
     */
    public List<TTask> selectTTaskList(TTask tTask);

    /**
     * 查询任务列表并返回任务明细
     * @param tTask
     * @return
     */
    public List<TTask> selectTTaskAndTaskUpsList(TTask tTask);

    /**
     * 查询任务列表并返回任务明细
     * @param tTask
     * @return
     */
    public List<TTask> surfingList(TTask tTask);

    /**
     * 新增任务
     * 
     * @param tTask 任务
     * @return 结果
     */
    public int insertTTask(TTask tTask);

    /**
     * 修改任务
     * 
     * @param tTask 任务
     * @return 结果
     */
    public int updateTTask(TTask tTask);

    /**
     * 删除任务
     * 
     * @param taskId 任务主键
     * @return 结果
     */
    public int deleteTTaskByTaskId(Long taskId);

    /**
     * 批量删除任务
     * 
     * @param taskIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTTaskByTaskIds(Long[] taskIds);

    /**
     * 根据任务标签获取任务及时长增加记录
     * @param taskId,taskContent,createBy
     * @return
     */
    public TTask selectTTaskByTaskContentAndCreater(@Param("taskId")Long taskId,@Param("taskContent")String taskContent, @Param("createBy")String createBy);

    /**
     * 获取用户的总任务时长
     * @return
     */
    public Long totalDurationMin(@Param("status")String status,@Param("createBy")String createBy);

    /**
     * 统计当天任务时长
     * @param tTask
     * @return
     */
    public List<TTask> selectTodayDuration(TTask tTask);

    @Select("SELECT a.update_time as cdate,a.sum_min,a.sum_noweight_min,a.sum_weight_min,b.sum_min as task_sum_min,b.weight,b.task_content" +
            " from (" +
            " SELECT DATE_FORMAT(u.create_time,'%Y-%m-%d') as update_time, " +
            " sum(u.up_min) as sum_min," +
            " sum(case when t.weight is null or t.weight = 0 then u.up_min else 0 end) as sum_noweight_min," +
            " sum(case when t.weight is null or t.weight = 0 then 0 else u.up_min end) as sum_weight_min" +
            " FROM t_task t left join t_task_up u on t.task_id=u.task_id WHERE DATE_FORMAT( u.create_time,'%Y-%m-%d') BETWEEN '${qt.startUpdateDate}' AND '${qt.endUpdateDate}' " +
            " AND t.create_by = '${qt.createBy}' AND t.status = '${qt.status}'" +
            " group by DATE_FORMAT(u.create_time,'%Y-%m-%d')" +
            " )a left join (" +
            " SELECT DATE_FORMAT(u.create_time,'%Y-%m-%d') as update_time,t.task_id,t.task_content,t.weight,sum(u.up_min) as sum_min" +
            " FROM t_task t left join t_task_up u on t.task_id=u.task_id WHERE DATE_FORMAT( u.create_time,'%Y-%m-%d') BETWEEN '${qt.startUpdateDate}' AND '${qt.endUpdateDate}' " +
            " AND t.create_by = '${qt.createBy}' AND t.status = '${qt.status}' and t.task_id='${qt.taskId}'" +
            " group by DATE_FORMAT(u.create_time,'%Y-%m-%d'),t.task_id,t.task_content,t.weight" +
            " )b on a.update_time = b.update_time" +
            " order by a.update_time")
    public List<WeightDriftRateVO> selectWeightDriftRate(@Param("qt") TTask queryTask);
}
